import { getObjectFromSessionStorage, setObjectToSessionStorage } from '@/utils'
import { FlowOperationController } from '@/api/flowController.js'

// 催办消息轮询间隔
const MESSAGE_TIMER_INTERVAL = 1000 * 60 * 5
// 获得消息列表数据
function loadMessage(context) {
  FlowOperationController.getMessageCount({}).then(res => {
    context.commit('SET_MESSAGE_COUNT', res.data)
  }).catch(e => {
    console.error(e)
  })
}

const state = {
  // 是否折叠
  isCollapse: false,
  // 是否多栏目
  supportColumn: false,
  // 是否多标签
  supportTags: true,
  // 缓存页面
  cachePages: getObjectFromSessionStorage('cachePages', []),
  // 在线表单查询页面缓存
  onlineFormCache: {},
  // 浏览器客户区高度
  documentClientHeight: 100,
  // 浏览器客户区宽度
  documentClientWidth: undefined,
  // 消息列表
  messageTimeer: null,
  messageCount: []
}

const mutations = {
  SET_COLLAPSE: (state, collapse) => {
    state.isCollapse = collapse
  },
  SET_SUPPORT_COLUMN: (state, supportColumn) => {
    state.supportColumn = supportColumn
  },
  SET_SUPPORT_TAGS: (state, supportTags) => {
    state.supportTags = supportTags
  },
  ADD_ONLINE_FORM_CACHE: (state, data) => {
    state.onlineFormCache[data.key] = data.value
  },
  REMOVE_ONLINE_FORM_CACHE: (state, key) => {
    delete state.onlineFormCache[key]
  },
  CLEAR_ONLINE_FORM_CACHE: (state) => {
    state.onlineFormCache = {}
  },
  ADD_CACHE_PAGE: (state, name) => {
    if (state.cachePages.indexOf(name) === -1) {
      var temp = [...state.cachePages]
      temp.push(name)
      setObjectToSessionStorage('cachePages', temp)
      state.cachePages = temp
    }
  },
  REMOVE_CACHE_PAGE: (state, name) => {
    var pos = state.cachePages.indexOf(name)
    if (pos !== -1) {
      var temp = [...state.cachePages]
      temp.splice(pos, 1)
      setObjectToSessionStorage('cachePages', temp)
      state.cachePages = temp
    }
  },
  CLEAR_CACHE_PAGE: (state) => {
    setObjectToSessionStorage('cachePages', [])
    state.cachePages = []
  },
  SET_CLIENT_HEIGHT: (state, height) => {
    state.documentClientHeight = height
  },
  SET_CLIEN_WIDTH: (state, width) => {
    state.documentClienWidth = width
  },
  SET_MESSAGE_TIMER: (state, timerHandler) => {
    state.messageTimer = timerHandler
  },
  SET_MESSAGE_COUNT: (state, messageCount) => {
    if (messageCount) {
      messageCount.totalCount = messageCount.copyMessageCount + messageCount.remindingMessageCount
    }
    state.messageCount = messageCount
  }
}

const actions = {
  setCollapse({ commit }, payload) {
    return new Promise(resolve => {
      commit('SET_COLLAPSE', payload)
      resolve()
    })
  },
  setSupportColumn({ commit }, payload) {
    return new Promise(resolve => {
      commit('SET_SUPPORT_COLUMN', payload)
      resolve()
    })
  },
  setSupportTags({ commit }, payload) {
    return new Promise(resolve => {
      commit('SET_SUPPORT_TAGS', payload)
      resolve()
    })
  },
  addOnlineFormCache({ commit }, data) {
    commit('ADD_ONLINE_FORM_CACHE', data)
  },
  removeOnlineFormCache({ commit }, key) {
    commit('REMOVE_ONLINE_FORM_CACHE', key)
  },
  clearOnlineFormCache({ commit, state }) {
    return new Promise(resolve => {
      commit('CLEAR_ONLINE_FORM_CACHE')
      resolve([...state.onlineFormCache])
    })
  },
  addCachePage({ commit }, name) {
    commit('ADD_CACHE_PAGE', name)
  },
  removeCachePage({ commit }, data) {
    commit('REMOVE_CACHE_PAGE', data)
  },
  clearCachePage({ commit, state }) {
    return new Promise(resolve => {
      commit('CLEAR_CACHE_PAGE')
      resolve([...state.cachePages])
    })
  },
  setClientHeight: ({ commit }, height) => {
    return new Promise(resolve => {
      commit('SET_CLIENT_HEIGHT', height)
      resolve()
    })
  },
  setClientWidth: ({ commit }, width) => {
    return new Promise(resolve => {
      commit('SET_CLIEN_WIDTH', width)
      resolve()
    })
  },
  setMessageTimer: ({ commit }, timerHandler) => {
    return new Promise(resolve => {
      commit('SET_MESSAGE_TIMER', timerHandler)
      resolve()
    })
  },
  setMessageCount: ({ commit }, messageCount) => {
    return new Promise(resolve => {
      commit('SET_MESSAGE_COUNT', messageCount)
      resolve()
    })
  },
  startMessage: (context) => {
    if (context.state.messageTimer !== null) {
      clearInterval(context.state.messageTimer)
    }
    var timer = setInterval(() => {
      loadMessage(context)
    }, MESSAGE_TIMER_INTERVAL)
    context.commit('SET_MESSAGE_TIMER', timer)
    loadMessage(context)
  },
  stopMessage: (context) => {
    if (context.state.messageTimer !== null) {
      clearInterval(context.state.messageTimer)
    }
    context.commit('SET_MESSAGE_TIMER', null)
  },
  reloadMessage: (context) => {
    loadMessage(context)
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
